package cn.yunhe.service;

import cn.yunhe.dao.NewsMapper;
import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import cn.yunhe.util.PageUtil;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class NewsService {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    NewsMapper newsMapper = session.getMapper(NewsMapper.class);

    public List<News> getNewsByPage(int currPage, String keyword) {
        Page page = new Page();
        page.setPageStart(PageUtil.PAGE_SIZE * (currPage - 1));   //pageSize*(currPage-1)
        page.setPageSize(PageUtil.PAGE_SIZE);
        page.setKeyword(keyword);

        List<News> newsList = newsMapper.getNewsByPage(page);
        return newsList;
    }

    public int getNewsCount() {
        return newsMapper.getNewsCount();
    }

    public boolean delNewsByNewsid(int newsid) {
        boolean flag = newsMapper.delNewsByNewsid(newsid);
        session.commit();
        return flag;
    }

    public News getNewsByNewsid(int newsid) {
        return newsMapper.getNewsByNewsid(newsid);
    }


    public boolean addNews(News news) {
        boolean flag = newsMapper.addNews(news);
        session.commit();
        return flag;
    }


}
